/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.univcasa.biz.impl;

import com.univcasa.biz.SyncApogeeComptesENT;
import com.univcasa.db.model.CompteEtudiant;
import com.univcasa.db.model.Criteres;
import com.univcasa.managing.ApogeeStudentsManager;
import com.univcasa.managing.ComptesStudentsManager;
import com.univcasa.managing.impl.ApogeeStudentsManagerImpl;
import com.univcasa.managing.impl.ComptesStudentsManagerImpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;


/**
 *
 * @author issam
 */
public class SyncApogeeComptesENTImpl implements SyncApogeeComptesENT{

    ApogeeStudentsManager apogeeManager;
    ComptesStudentsManager comptesManager;


    public SyncApogeeComptesENTImpl() {
        apogeeManager = new ApogeeStudentsManagerImpl();
        comptesManager = new ComptesStudentsManagerImpl();
    }

    //ajouter les comptes depuis apogee
    public List ajouterComptesENTdepuisApogee( Criteres criteres ){
        
        List listapogee = apogeeManager.getStdsFromApogeeByCriteria( criteres );
        List listCmpt2Add = new ArrayList();
        List addedComptes = new ArrayList();

        for (Iterator itr = listapogee.iterator() ; itr.hasNext();){
            CompteEtudiant compte = (CompteEtudiant) itr.next();
            String apogee = compte.getApogee();

            if ( !addedComptes.contains(apogee) && ! comptesManager.isExists(compte)){
                listCmpt2Add.add(compte);
                comptesManager.ajouterCompteEtudiant(compte);
                addedComptes.add(apogee);
            }
        }

        return listCmpt2Add;
    }

    public void majComptesENTdepuisApogee(Criteres criteres) {

        /*
        List listapogeecomptes = comptesManager.getAllApogeeComptesStudentsWithNoCin();
        Criteres form = new Criteres();
        form.put("annee", "%" ) ;
        form.put("etab", "%");
        form.put("apogee", "%" );
        
        for (Iterator itr = listapogeecomptes.iterator() ; itr.hasNext();){
            String apogee = (String) itr.next();
            Criteres form2 = new Criteres();
            form2.put("apogee", apogee);
            CompteEtudiant etuApo = apogeeManager.getStdCompteByCrieria(form);
            CompteEtudiant etudiant = comptesManager.getCompteEtudiantByApogee(apogee);
            etudiant.setCin( etuApo.getCin() );
            comptesManager.majCompteEtudiant(etudiant);                    
                }
        /*
        List listapogeecomptes = comptesManager.getAllApogeeComptesStudentsWithNoCin();
        Criteres form = new Criteres();
        form.put("annee", "%" ) ;
        form.put("etab", "%");
        form.put("apogee", "%" );
        Criteres apogeeCin = new Criteres();
        List listComptesApogee = apogeeManager.getStdsFromApogeeByAnneeInsc(form);
        for (Iterator itr = listComptesApogee.iterator() ; itr.hasNext();){
            CompteEtudiant etudApo = (CompteEtudiant) itr.next();
            apogeeCin.put(etudApo.getApogee(), etudApo.getCin());
        }

        System.out.println("itr apogee comptes");
        for (Iterator itr = listapogeecomptes.iterator() ; itr.hasNext();){
            String apogee = (String) itr.next();           
            CompteEtudiant etudiant = comptesManager.getCompteEtudiantByApogee(apogee);
            etudiant.setCin( (String)apogeeCin.get(apogee)   );
            comptesManager.majCompteEtudiant(etudiant);
                }*/
        }


    public HashMap simulerAjoutComptesENT2Apo(Criteres criteres) {
        List listapogee = apogeeManager.getStdsFromApogeeByCriteria( criteres );
        List listCmpt2Add = new ArrayList();
        List addedComptes = new ArrayList();
        int i =0 ;

        for (Iterator itr = listapogee.iterator() ; itr.hasNext();){
            CompteEtudiant compte = (CompteEtudiant) itr.next();
            String apogee = compte.getApogee();

            if ( !addedComptes.contains(apogee) && ! comptesManager.isExists(compte)){
                i++;
                listCmpt2Add.add(compte);
                addedComptes.add(apogee);
            }
        }
        HashMap result = new HashMap();
        result.put("i", Integer.valueOf(i) );
        result.put("list", listCmpt2Add);
        return result;
    }
    }
